package com.xgry.bpm.api.base.moduler.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xgry.bpm.api.base.moduler.dao.TBModulerDao;
import com.xgry.bpm.api.base.moduler.entity.TBModuler;
import com.xgry.bpm.api.base.moduler.form.TBModulerForm;
import com.xgry.bpm.api.base.moduler.service.TBModulerService;
import com.xgry.bpm.api.base.moduler.vo.TBModulerVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;


/**
 * <p>
 * TBModuler服务实现类
 * </p>
 *
 * @author admin
 * @since 2020-07-14
 */
@Service
public class TBModulerServiceImpl extends ServiceImpl<TBModulerDao, TBModuler> implements TBModulerService {

    @Override
    public int save(TBModulerForm record) {
        TBModuler data = this.convertDO(record);
        return baseMapper.insert(data);
    }

    @Override
    public int updateById(TBModulerForm record) {
        //修改时间
        TBModuler data = this.convertDO(record);
        return baseMapper.updateById(data);
    }

    @Override
    public int deleteById(String id) {
        return baseMapper.deleteById(id);
    }

    @Override
    public TBModulerVO selectById(String id) {
        TBModuler data = baseMapper.selectById(id);
        TBModulerVO vo = new TBModulerVO();
        BeanUtils.copyProperties(data, vo);
        return vo;
    }

    @Override
    public List<TBModulerVO> selectAll(TBModulerForm record) {
        TBModuler data = this.convertDO(record);
        QueryWrapper<TBModuler> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(data);
        List<TBModuler> list = baseMapper.selectList(queryWrapper);
        return this.convert(list);
    }

    @Override
    public IPage<TBModulerVO> selectPage(TBModulerForm record) {

        // form -> do 转换
        TBModuler data = this.convertDO(record);
        // 分页数据设置

        Page<TBModuler> page = new Page<>(record.getCurrent(), record.getSize());
        // 查询条件
        QueryWrapper<TBModuler> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(data);
        //queryWrapper.orderByAsc("i_moduler_sort");
        IPage<TBModuler> list = baseMapper.selectPage(page, queryWrapper);
        IPage<TBModulerVO> iPage = new Page<>();
        iPage.setRecords(this.convert(list.getRecords()));
        iPage.setCurrent(list.getCurrent());
        iPage.setSize(list.getSize());
        iPage.setTotal(list.getTotal());
        iPage.setPages(list.getPages());
        return iPage;
    }

    @Override
    public IPage<TBModuler> getPage(IPage<TBModuler> page, QueryWrapper<TBModuler> queryWrapper) {
        return baseMapper.selectPage(page, queryWrapper);
    }

    @Override
    public TBModuler findModelByName(String modulerName) {
        return baseMapper.selectOne(new QueryWrapper<TBModuler>(){{
            this.eq("v_moduler_name",modulerName);
        }});
    }

    @Override
    public TBModuler findModelByCode(String modulerCode) {
        return baseMapper.selectOne(new QueryWrapper<TBModuler>(){{
            this.eq("v_moduler_code",modulerCode);
        }});
    }

    @Override
    public TBModuler moveUpModuler(Integer modulerId) {
        return updateModulerSortUpOrDown(modulerId,true);
    }


    @Override
    public TBModuler moveDownModuler(Integer modulerId) {
        return updateModulerSortUpOrDown(modulerId,false);
    }


    private TBModuler updateModulerSortUpOrDown(Integer modulerId, Boolean isUp){
        TBModuler tbModuler = baseMapper.selectById(modulerId);
        List<TBModuler> tbModulers = baseMapper.selectList(new QueryWrapper<TBModuler>() {{
            if (isUp) {
                this.le("modularOrderId", tbModuler.getIModulerSort());
                this.orderByDesc("modularOrderId");
            }else{
                this.ge("modularOrderId", tbModuler.getIModulerSort());
                this.orderByAsc("modularOrderId");
            }
        }});

        if (CollectionUtils.isEmpty(tbModulers)) {
            return null;
        }

        //交换排序号
        TBModuler preModuler = tbModulers.get(0);
        Integer iModulerSort = preModuler.getIModulerSort();
        preModuler.setIModulerSort(tbModuler.getIModulerSort());
        tbModuler.setIModulerSort(iModulerSort);

        baseMapper.updateById(tbModuler);
        baseMapper.updateById(preModuler);
        return tbModuler;
    }


    /**
     * Form -> Do
     *
     * @param form 对象
     * @return Do对象
     */
    private TBModuler convertDO(TBModulerForm form) {
        TBModuler data = new TBModuler();
        BeanUtils.copyProperties(form, data);
        return data;
    }

    /**
     * Do -> VO
     *
     * @param list 对象
     * @return VO对象
     */
    private List<TBModulerVO> convert(List<TBModuler> list) {
        List<TBModulerVO> tBModulerList = new ArrayList<>();
        if (CollectionUtils.isEmpty(list)) {
            return tBModulerList;
        }
        for (TBModuler source : list) {
            TBModulerVO target = new TBModulerVO();
            BeanUtils.copyProperties(source, target);
            tBModulerList.add(target);
        }
        return tBModulerList;
    }

}
